home *** CD-ROM | disk | FTP | other *** search
- Path: news.mcgill.ca!noyb
- From: noyb@ee470.ee.mcgill.ca (Terence Chiu)
- Newsgroups: comp.lang.c
- Subject: new C programmer needs help with quicksort
- Date: 8 Feb 1996 04:57:17 GMT
- Organization: McGill University, Undergraduate EE Lab
- Message-ID: <4fbvrd$i6p@sifon.cc.mcgill.ca>
- NNTP-Posting-Host: joker.ee.mcgill.ca
-
- Hi, I am a new C programmer and I have to right an assignment that sorts an
- array in ascending order. The algorithm that I am required to write is that I
- must use an auxiliary array instead of repeated exchanges: here is the code
- #include <stdio.h>
- void quicksort( int table[] ,int first,int last);
- void main ()
- { int table[3];
- table[0]=3;
- table[1]=2;
- table[2]=1;
- /*table[3]=2;
- table[4]=1;
- table[5]=4;
- table[6]=5;*/
- quicksort(table,0,2);
- }
- void quicksort( int table[] ,int first,int last)
- {int aux[3];
- int idxhigh, idxlow;
- int pivot, test, temp,i;
- pivot=table[first];
- idxhigh=last; idxlow=first;
-
- for (i=first+1; i<=last; i=i+1)
- {test=table[i];
- if (test < pivot)
- {aux[idxlow]=test;
- idxlow = idxlow+1;}
- if (test > pivot)
- {aux[idxhigh]=test;
- idxhigh = idxhigh-1;}
- }
- aux[idxlow]=pivot;
-
- quicksort(aux,first,idxlow-1);
- quicksort(aux,idxhigh+1,last);
- }
-
- for some when the function calls on itself the aux array loses all its current
- data. Why?
-